<?php

namespace App\Http\Middleware;

use App\Exceptions\ApiException;
use App\Services\UserService;
use Closure;
use Illuminate\Http\Request;

class CheckLogin
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle(Request $request, Closure $next)
    {
        $service = new UserService();
        if ($service->checkLogin(
            $request->input('userId') ?? null,
            $request->input('token') ?? null
        )) {
            return $next($request);
        }
        throw new ApiException('未登录', 901);
    }
}
